//
//  LLNewFeatureViewController.swift
//  WeiBoShiBing
//
//  Created by Bingo on 15/10/30.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class LLNewFeatureViewController: UICollectionViewController {
    
    private var layOut = UICollectionViewFlowLayout()
    private var itemCount = 4

    init() {
        
        super.init(collectionViewLayout:layOut)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(LLNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        prepareLayout()
    }
    
    private func prepareLayout(){
        layOut.itemSize = UIScreen.mainScreen().bounds.size
        layOut.minimumInteritemSpacing = 0
        layOut.minimumLineSpacing = 0
        
        layOut.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return itemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as!LLNewFeatureCell
        
        // Configure the cell
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let showIndexPath = collectionView.indexPathsForVisibleItems().first!
        
        let cell = collectionView.cellForItemAtIndexPath(showIndexPath)as!LLNewFeatureCell
        
        if showIndexPath.item == itemCount - 1{
            
            cell.startButtonAnimation()
        }
    }
}

class LLNewFeatureCell:UICollectionViewCell{
    
    
    var imageIndex:Int = 0 {
        didSet{
            startButton.hidden = true
            backgroundImageView.image = UIImage(named: "new_feature_\(imageIndex+1)")
            print("\(imageIndex)")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func startButtonAnimation(){
        
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    private func prepareUI(){
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(startButton)
        
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
//    contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bak]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bak" : backgroundImageView]))
//        
//        contentView.addConstraints(  NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bak]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bak" : backgroundImageView]))
        
        backgroundImageView.ff_Fill(contentView)
        
        
//        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
        
    }
    
     func startButtonClick(){
        
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(true)
        
        
    }
    private lazy var backgroundImageView = UIImageView()
    
    private lazy var startButton:UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    
}










